package com.bocai.yuweiwangluo.activity;


import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.RoundImageView;
import com.bocai.yuweiwangluo.adapter.ParticipantAdapter;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.JoinPersonBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.TimeUtils;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 参与者界面
 */
public class ParticipantsActivity extends BaseActivity implements View.OnClickListener ,AdapterView.OnItemClickListener{

    @ViewInject(R.id.lv_participant_list)
    ListView lv_participant_list;
    @ViewInject(R.id.ll_row)
    LinearLayout llRow;
    @ViewInject(R.id.riv_photo)
    RoundImageView rivPhoto;
    @ViewInject(R.id.tv_name)
    TextView tvName;
    @ViewInject(R.id.tv_time)
    TextView tvTime;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private JoinPersonBean joinPersonBean;

    @Override
    protected int contentView() {
        return R.layout.activity_participants;
    }

    @Override
    protected void setData() {
        ViewUtils.inject(this);
        initToolbar("参与者");
        lv_participant_list.setFocusable(false);
        getData();
        initEvent();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        llRefresh.setOnClickListener(this);
        llRow.setOnClickListener(this);
        lv_participant_list.setOnItemClickListener(this);
    }

    private void getData() {
        showLoading();
        String id = getIntent().getStringExtra("id");
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(this, this);
        myOkHttpClient.getMatchJoin(UrlData.BASEURL + UrlData.GETMATCHJOIN, "2", handler);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                getData();
                break;
            case R.id.ll_row:
                try{
                    if(joinPersonBean.getUsr_row().getMobile()!=null){
                        Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+joinPersonBean.getUsr_row().getMobile()));
                        startActivity(intent);
                    }

                }catch (Exception e){}
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            hideLoading();
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try {
                switch (msg.what) {
                    case RESPONSE_SUCCEED:
                        Log.e("result", result);
                        joinPersonBean = gson.fromJson(result, JoinPersonBean.class);
                        setDatatoView();
                        break;
                    case RESPONSE_FAILED:
                        showError(result);
                        llRefresh.setVisibility(View.VISIBLE);
                        break;
                }
            } catch (Exception e) {
            }
        }

    };

    private void setDatatoView() {

        try {
            ImageLoaderUtil.loaderImageDefault(UrlData.IMAGEURL+joinPersonBean.getUsr_row().getIcon(),rivPhoto);
            tvName.setText(joinPersonBean.getUsr_row().getNickname() + "约战");
            tvTime.setText(TimeUtils.getTime_china(Long.parseLong(joinPersonBean.getUsr_row().getTimeline())));
            ParticipantAdapter mParticipantAdapter=new ParticipantAdapter(this,joinPersonBean);
            lv_participant_list.setAdapter(mParticipantAdapter);
        }catch (Exception e){

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try{

            if(joinPersonBean.getUsrs_join().get(position).getMobile()!=null){
                Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+joinPersonBean.getUsrs_join().get(position).getMobile()));
                startActivity(intent);
            }
        }catch (Exception e){}
    }
}
